﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

using System.Drawing;
using CE.Database;
using CE.Global;

public partial class user_Calendar : System.Web.UI.Page
{
    protected DataSet dsEvents;
    string strViewStateKey = "vHeaderValues";
    string strViewStateEventKey = "vEventKey";
    string strViewStateMonthEventKey = "vViewMonthEventKey";
    string strViewStateWeekKey = "vWeekKey";
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        ViewState[strViewStateKey] = null;
        if (!IsPostBack)
        {
            ViewState[strViewStateWeekKey] = null;
            ViewState[strViewStateMonthEventKey] = null;
            Calendar1.VisibleDate = dateTimeNow.AddDays(0);
            FillEventDataset();
        }
    }

    protected void FillEventDataset()
    {
        if (ViewState[strViewStateMonthEventKey] == null)
        {
            DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year,
                Calendar1.VisibleDate.Month, 1);
            DateTime lastDate = GetFirstDayOfNextMonth();
            dsEvents = GetCurrentMonthData(firstDate, lastDate);
            ViewState[strViewStateMonthEventKey] = dsEvents;
        }
        else
        {
            dsEvents = (DataSet)ViewState[strViewStateMonthEventKey];
        }
    }

    protected DateTime GetFirstDayOfNextMonth()
    {
        int monthNumber, yearNumber;
        if (Calendar1.VisibleDate.Month == 12)
        {
            monthNumber = 1;
            yearNumber = Calendar1.VisibleDate.Year + 1;
        }
        else
        {
            monthNumber = Calendar1.VisibleDate.Month + 1;
            yearNumber = Calendar1.VisibleDate.Year;
        }
        DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
        return lastDate;
    }

    protected DataSet GetCurrentMonthData(DateTime firstDate,
         DateTime lastDate)
    {
        DataSet dsMonth = new DataSet();
        DataTable _dtEvents = new DataTable("tblEvents");
        DataTable _dtCallStatus = new DataTable("tblCallStatus");

        MyDatabase _db = new MyDatabase();
        string spName = "sprocCXCRMAgentCalenderGetEvents";
        SortedList paraList = new SortedList();
        paraList.Add("@StartDate", firstDate.ToString("yyyyMMdd"));
        paraList.Add("@EndDate", lastDate.ToString("yyyyMMdd"));
        _dtEvents = _db.GetDataTableBySP(spName, paraList);

        paraList.Clear();
        spName = "sprocCXCRMAgentCalenderGetCallStatus";
        paraList.Add("@FromDate", firstDate.ToString("yyyyMMdd"));
        paraList.Add("@ToDate", lastDate.ToString("yyyyMMdd"));
        paraList.Add("@AgentID", Convert.ToInt64(Session[General.UserId].ToString()));
        _dtCallStatus = _db.GetDataTableBySP(spName, paraList);

        dsMonth.Tables.Add(_dtEvents);
        dsMonth.Tables.Add(_dtCallStatus);

        return dsMonth;
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime nextDate = e.Day.Date;

        if (dsEvents != null)
        {
            HyperLink hplCallStatus = new HyperLink();
            hplCallStatus.Text = string.Empty;

            string _callDate = string.Empty;
            int iYear;
            int iMonth;
            int iDay;

            string strCB = string.Empty;
            string strFU = string.Empty;
            string strEV = string.Empty;

            foreach (DataRow dr in dsEvents.Tables[1].Rows)
            {
                _callDate = dr["CallDate"].ToString();
                iYear = Convert.ToInt32(_callDate.Substring(0, 4));
                iMonth = Convert.ToInt32(_callDate.Substring(4, 2));
                iDay = Convert.ToInt32(_callDate.Substring(6, 2));

                nextDate = new DateTime(iYear, iMonth, iDay);

                if (nextDate == e.Day.Date)
                {
                    if (dr["CallFinishType"].ToString() == "3")
                    {
                        strFU += "<font color='green' weight='bold'>FU: " + dr["CallCount"].ToString() + "</font>";
                    }
                    else if (dr["CallFinishType"].ToString() == "2")
                    {
                        strCB += "<font color='red' weight='bold'>CB: " + dr["CallCount"].ToString() + "</font>";
                    }
                }
            }

            foreach (DataRow dr in dsEvents.Tables[0].Rows)
            {
                nextDate = (DateTime)dr["EventDate"];
                if (nextDate == e.Day.Date)
                {
                    strEV = "<font size='x-small' color='black' weight='bold'>" + dr["EventName"].ToString() + "</font>";
                    e.Cell.ToolTip = dr["EventName"].ToString();
                }
            }

            if (strEV != string.Empty)
            {
                hplCallStatus.Text = "<br/>" + strEV;
            }

            if (strCB != string.Empty || strFU != string.Empty)
            {
                hplCallStatus.Text += "<br/>" + strCB + "&nbsp;" + strFU;
            }


            hplCallStatus.NavigateUrl = "~/carinsurance/ViewAgentCallStatus.aspx?viewdate=" + e.Day.Date.ToString("yyyyMMdd");
            e.Cell.Controls.AddAt(1, hplCallStatus);

            if (strEV != string.Empty)
            {
                e.Cell.BackColor = Color.LightPink;
            }
            else if (strCB != string.Empty || strFU != string.Empty)
            {
                if (e.Day.Date.DayOfWeek == DayOfWeek.Saturday || e.Day.Date.DayOfWeek == DayOfWeek.Sunday)
                {
                    e.Cell.BackColor = System.Drawing.Color.DarkGray;
                }
                else
                {
                    e.Cell.BackColor = System.Drawing.Color.LightGreen;
                }

                if (e.Day.Date == General.GetUTCDateTimeToday()) //DateTime.Today
                {
                    e.Cell.BackColor = System.Drawing.Color.Yellow;
                }
            }

            if (hplCallStatus.Text == string.Empty)
            {
                e.Cell.Text = e.Day.Date.Day.ToString();
            }
        }
    }
    protected void Calendar1_VisibleMonthChanged(object sender,
        MonthChangedEventArgs e)
    {
        ViewState[strViewStateMonthEventKey] = null;
        FillEventDataset();
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        if (Calendar1.SelectedDate != null)
        {
            Response.Redirect("~/carinsurance/ViewAgentCallStatus.aspx?viewdate=" + Calendar1.SelectedDate.ToString("yyyyMMdd"));
        }
    }

    private void FillWeekData(DateTime StartDate, DateTime EndDate)
    {
        DataTable _dtWeek = null;

        _dtWeek = new DataTable("tblWeek");
        CE.Database.MyDatabase _db = new CE.Database.MyDatabase();
        System.Collections.SortedList _sPara = new System.Collections.SortedList();
        _sPara.Add("@StartDate", StartDate);
        _sPara.Add("@EndDate", EndDate);
        _sPara.Add("@AgentID", Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));
        _dtWeek = _db.GetDataTableBySP("sprocCXCRMAgentCalendarWeekView", _sPara);

        ViewState[strViewStateWeekKey] = _dtWeek;

        DataTable _dtEvents = new DataTable("tblEvents");
        _sPara.Clear();
        _sPara.Add("@StartDate", StartDate.ToString("yyyyMMdd"));
        _sPara.Add("@EndDate", EndDate.ToString("yyyyMMdd"));
        _dtEvents = _db.GetDataTableBySP("sprocCXCRMAgentCalenderGetEvents", _sPara);

        ViewState[strViewStateEventKey] = _dtEvents;
    }

    protected void ddlViewType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlViewType.Text != "1")
        {
            Calendar1.Visible = false;
            GridView1.Visible = true;

            DataTable _dtWeek = null;
            DataTable _dtTemp = null;
            //sprocCXCRMAgentCalendarWeekView
            if (ViewState[strViewStateWeekKey] == null)
            {
                DateTime tmpStartDate = dateTimeNow;
                DateTime tmpEndDate = dateTimeNow;

                tmpStartDate = dateTimeNow.AddDays(-2);
                tmpEndDate = dateTimeNow.AddDays(4);

                FillWeekData(tmpStartDate, tmpEndDate);
            }

            if (ddlViewType.Text == "2")
            {
                _dtTemp = new DataTable("tblTemp");
                _dtWeek = new DataTable("tblWeek");
                _dtWeek = (DataTable)ViewState[strViewStateWeekKey];

                _dtTemp.Columns.Add(_dtWeek.Columns[0].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[1].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[2].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[3].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[4].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[5].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[6].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[7].ColumnName);
                foreach (DataRow row in _dtWeek.Rows)
                {
                    DataRow _newRow = _dtTemp.NewRow();
                    _newRow[0] = row[0];
                    _newRow[1] = row[1];
                    _newRow[2] = row[2];
                    _newRow[3] = row[3];
                    _newRow[4] = row[4];
                    _newRow[5] = row[5];
                    _newRow[6] = row[6];
                    _newRow[7] = row[7];
                    _dtTemp.Rows.Add(_newRow);
                }
            }
            else if (ddlViewType.Text == "3")
            {
                _dtTemp = new DataTable("tblToday");
                _dtWeek = new DataTable("tblWeek");
                _dtWeek = (DataTable)ViewState[strViewStateWeekKey];

                _dtTemp.Columns.Add(_dtWeek.Columns[0].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[3].ColumnName);
                foreach (DataRow row in _dtWeek.Rows)
                {
                    DataRow _newRow = _dtTemp.NewRow();
                    _newRow[0] = row[0];
                    _newRow[1] = row[3];
                    _dtTemp.Rows.Add(_newRow);
                }
            }
            else if (ddlViewType.Text == "4")
            {
                _dtTemp = new DataTable("tblFourDay");
                _dtWeek = new DataTable("tblWeek");
                _dtWeek = (DataTable)ViewState[strViewStateWeekKey];

                _dtTemp.Columns.Add(_dtWeek.Columns[0].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[2].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[3].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[4].ColumnName);
                _dtTemp.Columns.Add(_dtWeek.Columns[5].ColumnName);
                foreach (DataRow row in _dtWeek.Rows)
                {
                    DataRow _newRow = _dtTemp.NewRow();
                    _newRow[0] = row[0];
                    _newRow[1] = row[2];
                    _newRow[2] = row[3];
                    _newRow[3] = row[4];
                    _newRow[4] = row[5];
                    _dtTemp.Rows.Add(_newRow);
                }
            }

            if (_dtTemp != null && _dtTemp.Rows.Count > 0)
            {
                GridView1.DataSource = _dtTemp;
                GridView1.DataBind();
            }

            _dtWeek.Dispose();
            _dtTemp.Dispose();
        }
        else
        {
            Calendar1.Visible = true;
            GridView1.Visible = false;
            GridView1.DataSource = null;
            GridView1.DataBind();
            FillEventDataset();
        }


    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string strTime = e.Row.Cells[0].Text;
            //9:00AM to 10:00AM
            string[] strTimeList = strTime.Split(' ');
            DateTime _dtTemp = Convert.ToDateTime(dateTimeNow.ToString("dd/MMM/yyyy") + " " + strTimeList[0]);

            string strHeaderValues = string.Empty;

            if (ViewState[strViewStateKey] == null)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    strHeaderValues += GridView1.HeaderRow.Cells[i].Text + ";";
                }
                ViewState[strViewStateKey] = strHeaderValues;
            }
            else
            {
                strHeaderValues = ViewState[strViewStateKey].ToString();
            }

            string[] arrHeaderValues = strHeaderValues.Split(';');

            for (int i = 1; i < e.Row.Cells.Count; i++)
            {
                string strDate = arrHeaderValues[i];
                string strEV = string.Empty;
                string strEVName = string.Empty;
                DateTime dtViewDate = Convert.ToDateTime(strDate);
                strEVName = dtViewDate.ToString("dddd, dd-MMM-yyyy");
                DateTime dtEventDate = dateTimeNow;
                if (ViewState[strViewStateEventKey] != null)
                {
                    DataTable dtEvents = new DataTable("tblEvents");
                    dtEvents = (DataTable)ViewState[strViewStateEventKey];
                    foreach (DataRow dr in dtEvents.Rows)
                    {
                        dtEventDate = (DateTime)dr["EventDate"];
                        if (dtEventDate.Date == dtViewDate.Date)
                        {
                            strEV += " (E)";
                            strEVName = dr["EventName"].ToString();
                        }
                    }
                }

                if (strEV != string.Empty)
                {
                    GridView1.HeaderRow.Cells[i].Text = dtViewDate.ToString("ddd dd-MMM") + strEV;
                    GridView1.HeaderRow.Cells[i].BackColor = Color.Green;
                }
                else
                {
                    GridView1.HeaderRow.Cells[i].Text = dtViewDate.ToString("ddd dd-MMM");
                }

                GridView1.HeaderRow.Cells[i].ToolTip = strEVName;

                if (e.Row.Cells[i].Text != "0")
                {
                    Panel mainPanel = new Panel();
                    HyperLink hplClickHere = new HyperLink();
                    hplClickHere.Text = e.Row.Cells[i].Text;
                    hplClickHere.NavigateUrl = "~/carinsurance/viewagentcallstatus.aspx?viewdate=" + dtViewDate.ToString("yyyyMMdd") + "&viewtime=" + _dtTemp.Hour.ToString();
                    mainPanel.Controls.Add(hplClickHere);
                    int _callCount = Convert.ToInt32(e.Row.Cells[i].Text);
                    if (_callCount > 5 && _callCount < 10)
                    {
                        hplClickHere.ForeColor = Color.Brown;
                    }
                    else if (_callCount >= 10)
                    {
                        hplClickHere.ForeColor = Color.Red;
                    }

                    mainPanel.BackColor = Color.LightPink;
                    e.Row.Cells[i].Controls.Add(mainPanel);
                }
            }
        }

        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TableCell Cell = e.Row.Cells[i];

            // if both row and column are odd, color then black
            // if both row and column are even, color then yellow
            if (((e.Row.RowIndex % 2 == 1) && (i % 2 == 1)) ||
                ((e.Row.RowIndex % 2 == 0) && (i % 2 == 0)))
                Cell.BackColor = System.Drawing.Color.White;
            else
                Cell.BackColor = System.Drawing.Color.WhiteSmoke;
        }
    }

    protected void rdoViewType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlViewType.Text = rdoViewType.Text;
        ddlViewType_SelectedIndexChanged(sender, e);
    }
}
